package org.djw.pet.service.impl;

import org.djw.basic.service.impl.BaseServiceImpl;
import org.djw.basic.util.CodeGenerateUtils;
import org.djw.basic.util.DistanceUtil;
import org.djw.basic.util.PageList;
import org.djw.basic.util.Point;
import org.djw.order.domain.PetAcquisitionOrder;
import org.djw.order.mapper.PetAcquisitionOrderMapper;
import org.djw.org.domain.Employee;
import org.djw.org.domain.Shop;
import org.djw.org.mapper.EmployeeMapper;
import org.djw.org.mapper.ShopMapper;
import org.djw.pet.domain.Pet;
import org.djw.pet.domain.SearchMasterMsg;
import org.djw.pet.mapper.PetMapper;
import org.djw.pet.mapper.SearchMasterMsgMapper;
import org.djw.pet.query.SearchMasterMsgQuery;
import org.djw.pet.service.ISearchMasterMsgService;
import org.djw.user.domain.Logininfo;
import org.djw.user.domain.User;
import org.djw.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetServiceImpl petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Logininfo logininfo) {
        //绑定登录人
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        searchMasterMsg.setUser_id(user.getId());
        //寻找最近的店铺进行绑定
        //获取经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //获取最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        //进行绑定
        searchMasterMsg.setShop_id(nearestShop.getId());
        //保存数据
        searchMasterMsgMapper.save(searchMasterMsg);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Logininfo Logininfo) {
        //根据登录信息获取用户
        User user = userMapper.loadByLogininfoId(Logininfo.getId());
        if (user != null){
            //将id设置给query
            query.setUserId(user.getId());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo Logininfo) {
        query.setState(0);//待处理
        Employee employee = employeeMapper.loadByLogininfoId(Logininfo.getId());
        if (employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo Logininfo) {
        query.setState(1);//已经处理
        Employee employee = employeeMapper.loadByLogininfoId(Logininfo.getId());
        if (employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public void handle(Pet pet, Logininfo currentLogininfo) {
        //修改寻主消息状态为已处理
        searchMasterMsgMapper.updateState2Processed(pet.getSearch_master_msg_id());
        //生成宠物信息和详情
        petService.add(pet);
        //生成订单
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadByLogininfoId(currentLogininfo.getId());
        PetAcquisitionOrder acquisitionOrder = pet2order(pet, searchMasterMsg, employee.getId());
        petAcquisitionOrderMapper.save(acquisitionOrder);

        //@TODO 生成支付单,并调用支付
    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt, Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
